import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/user_api.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';

import '../../widgets/base/qkd_text.dart';

class MyInviterPage extends StatefulWidget {
  const MyInviterPage({super.key});

  @override
  State<MyInviterPage> createState() => _MyInviterPageState();
}

class _MyInviterPageState extends State<MyInviterPage> {
  TextEditingController controller = TextEditingController();
  //1我的页面跳转
  int? type = Get.arguments;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return type == 1;
      },
      child: Scaffold(
        backgroundColor: Colours.scaffoldBg,
        appBar: Styles.appBar(
          showLeading: type == 1,
          backgroundColor: Colours.transparent,
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 32),
            child: Column(children: [
              const SizedBox(height: 8),
              Image.asset(
                Utils.getImgPath('inviter_header.png'),
                width: 185,
                height: 185,
              ),
              const SizedBox(height: 16),
              QkdText(
                '输入邀请人',
                fontSize: 16,
                fontWeight: FontWeight.w500,
              ),
              const SizedBox(height: 4),
              QkdText('领取限时专属优惠', color: Colours.grey66),
              const SizedBox(height: 32),
              TextField(
                controller: controller,
                // focusNode: _focusNode,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                        borderSide: BorderSide.none,
                        borderRadius: BorderRadius.circular(8)),
                    hintText: '输入邀请人名称-可不填',
                    fillColor: Colours.white,
                    filled: true,
                    hintStyle: const TextStyle(color: Colours.grey99, fontSize: 14),
                    contentPadding: const EdgeInsets.symmetric(horizontal: 16)),
                // textAlign: TextAlign.right,
                keyboardType: TextInputType.text,
                // style: TextStyleUtils.commonTextStyle,
              ),
              const SizedBox(height: 32),
              QkdButton.large(
                height: 48,
                onPressed: onSubmit,
                borderRadius: 8,
                text: '确认',
              ),
              const SizedBox(height: 20),
              if (type != 1)
                QkdButton.large(
                  onPressed: onJump,
                  height: 48,
                  text: '跳过',
                  textStyle: const TextStyle(
                      color: Colours.grey99,
                      fontSize: 14,
                      fontWeight: FontWeight.w400),
                  // foregroundColor: Colours.grey99,
                  backgroundColor: Colours.transparent,
                ),
            ]),
          ),
        ),
      ),
    );
  }

  onSubmit() async {
    if (controller.text.isEmpty) {
      ToastUtils.show('请输入邀请人名称');
      return;
    }
    final result = await UserApi.myInviter(controller.text);
    if (result == 200) {
      ToastUtils.show('填写成功');
      Get.back();
    }
  }

  onJump() {
    Get.back();
  }
}
